//! [0]
#include <QtGui/QtGui>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qrencode.h"

//! [1]
MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
	ui(new Ui::MainWindow)
//! [1] //! [2]
{
	ui->setupUi(this);
	setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint );
	QRcode *qrcode; //
	//QR_ECLEVEL_Q 
	qrcode = QRcode_encodeString("www.baidu.com", 2, QR_ECLEVEL_Q, QR_MODE_8, 1);
	qint32 temp_width=ui->label->width(); //
	qint32 temp_height=ui->label->height();
	qint32 qrcode_width = qrcode->width > 0 ? qrcode->width : 1;
	double scale_x = (double)temp_width / (double)qrcode_width; //
	double scale_y =(double) temp_height /(double) qrcode_width;
	QImage mainimg=QImage(temp_width,temp_height,QImage::Format_ARGB32);
	QPainter painter(&mainimg);
	QColor background(Qt::white);
	painter.setBrush(background);
	painter.setPen(Qt::NoPen);
	painter.drawRect(0, 0, temp_width, temp_height);
	QColor foreground(Qt::black);
	painter.setBrush(foreground);
	for( qint32 y = 0; y < qrcode_width; y ++)
	{
		for(qint32 x = 0; x < qrcode_width; x++)
		{
			unsigned char b = qrcode->data[y * qrcode_width + x];
			if(b & 0x01)
			{
				QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);
				painter.drawRects(&r, 1);
			}
		}
	}
	QPixmap mainmap=QPixmap::fromImage(mainimg);
	ui->label->setPixmap(mainmap);
	ui->label->setVisible(true);
}
//! [2]

MainWindow::~MainWindow()
{
	
}